package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.CustomizeBulletinDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.CustomizeBulletinMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class CustomizeBulletinDAO extends  BaseDAO<CustomizeBulletinDO>{
    @Resource
    private CustomizeBulletinMapper customizeBulletinMapper;

    @PostConstruct
    public void init(){
        this.mapper = customizeBulletinMapper;
    }

    public List<CustomizeBulletinDO> selectAllAndShow() {
        QueryWrapper<CustomizeBulletinDO> wrapper = new QueryWrapper<>();
        wrapper.eq("is_show",1).orderByDesc("create_time");
        return  mapper.selectList(wrapper);
    }

    public IPage<CustomizeBulletinDO> selectByPage(Pagination pagination) {
        QueryWrapper<CustomizeBulletinDO> wrapper = new QueryWrapper<>();
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<CustomizeBulletinDO> selectByPageAndName(Pagination pagination,String name) {
        QueryWrapper<CustomizeBulletinDO> wrapper = new QueryWrapper<>();
        wrapper.like("title",name);
        wrapper.like("content",name);
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public CustomizeBulletinDO selectByTitle() {
        QueryWrapper<CustomizeBulletinDO> wrapper = new QueryWrapper<>();
        wrapper.eq("title","补充信息");
        return  mapper.selectOne(wrapper);
    }
}
